<template>


    <el-button type="primary"  @click="handleAdd">
       新增轮播图
      </el-button>
    <el-table :data="tableData" style="width: 100%">
        <el-table-column label="轮播标题" prop="carouselTitle" />
        <el-table-column label="轮播图片" prop="carouselPath" >
          <template width="90" #default="scope">
          <img style="width:80px;height:80px;border:none;" :src="getServerUrl()+'/'+scope.row.carouselPath">
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" />
        <el-table-column align="right">
          <template #header>
        <el-input v-model="queryForm.query"  @input="initUserList" placeholder="请输入内容"></el-input>
          </template>
          <template #default="scope">
            <el-button size="small"  type="primary" @click="handleEdit(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button
              size="small"
              type="primary"
              @click="handleDelete(scope.row.id)" >
             删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="queryForm.pageNum"
          v-model:page-size="queryForm.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :size="size"
    
          background layout="prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
    
        <el-dialog v-model="dialogFormVisible" title="新增轮播" width="500">

  <el-form :model="carouselFrom" :rules="rules" ref="carouselFromRef" label-width="auto" style="max-width: 600px">
   
   <el-form-item prop="carouselTitle" label="轮播标题">
     <el-input v-model="carouselFrom.carouselTitle" placeholder="请输入轮播标题"></el-input>
   </el-form-item>
   <el-form-item prop="carouselPath" label="轮播图片">
    <el-input v-model="carouselFrom.carouselPath" placeholder="请上传轮播图"></el-input>
    <el-upload
    class="avatar-uploader"
    :action="getServerUrl()+'/carousel/uploadInco'"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <img v-if="imageUrl" :src="imageUrl" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
   </el-form-item>
  
  
 </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="resetForm">取消</el-button>
            <el-button type="primary" @click=" submitForm()">
              提交
            </el-button>
          </div>
        </template>
      </el-dialog>
    
    </template>
    
    <script  setup>
         import {ref,reactive} from 'vue';
         import request ,{getServerUrl} from '@/util/request';
         import { ElMessage ,ElMessageBox} from 'element-plus'
         import { Plus } from '@element-plus/icons-vue'
         const dialogFormVisible = ref(false)
         const tableData=ref([]);
         const total=ref(0);
         const queryForm=ref({
            query:"",
            pageNum:1,
            pageSize:10
         })
         const initUserList=async ()=>{
           const res= await request.post("/carousel/list",queryForm.value)
           tableData.value=res.data.data.dataList;
           total.value=res.data.data.total;
         }
         initUserList();
         const handleSizeChange=(pageSize)=>{
            queryForm.value.pageNum=1;
            queryForm.value.pageSize=pageSize
            initUserList();
         }
         const handleCurrentChange=(pageNum)=>{
            queryForm.value.pageNum=pageNum;
            initUserList();
         }
     
         const carouselFrom = reactive({
          id:'',
          carouselPath:'',
          carouselTitle:''
    })
    const imageUrl=ref("")
    const carouselFromRef = ref(null);
    const rules = {
      carouselPath: [
        { required: true, message: '请上传轮播图', trigger: 'blur' }
      ],
      carouselTitle: [
        { required: true, message: '请填写轮播标题', trigger: 'blur' }
      ]
    };



    const handleDelete=(id)=>{
  ElMessageBox.confirm(
          '确认删除',
          'Warning',
          {
            confirmButtonText: 'OK',
            cancelButtonText: 'Cancel',
            type: 'warning',
          }
      ).then(() => {
          request.get("carousel/"+id).then(res=>{
            if(res.data.code==200){
              initUserList()
              ElMessage.success("删除成功")
            }else{
              ElMessage.error(res.data.msg);
            }
          })
    })}





     
    const submitForm = () => {
      carouselFromRef.value.validate((valid) => {
        if (valid) {
          request.post("carousel/saveOrUpdate",carouselFrom).then(res=>{
           if(res.data.code==200){
            initUserList();
            resetForm();
            ElMessage.success("新增成功")
           }else{
            ElMessage.error(res.data.msg);
           }
          })
        } else {
          return false;
        }
      });
    };
    const handleEdit=(index,row)=>{
     let obj=  Object.assign({}, row)
     carouselFrom.id=obj.id;
     carouselFrom.carouselPath=obj.carouselPath;
     carouselFrom.carouselTitle=obj.carouselTitle;
     imageUrl.value= getServerUrl()+'/'+obj.carouselPath;
    dialogFormVisible.value = true
    }
    
    const handleAdd=()=>{
      dialogFormVisible.value = true
    }
    
    const resetForm = () => {
      carouselFrom.id='';
     carouselFrom.carouselPath='';
     carouselFrom.carouselTitle='';
     imageUrl.value= '';
          dialogFormVisible.value = false
        }

const handleAvatarSuccess= (res)=>{
  imageUrl.value= getServerUrl()+'/'+res.data.src
  carouselFrom.carouselPath=res.data.src;
   
}


const beforeAvatarUpload = (file) => {
  const isJPG = file.type === 'image/jpeg'
  const isLt2M = file.size / 1024 / 1024 < 2

  if (!isJPG) {
    ElMessage.error('图片必须是jpg格式')
  }
  if (!isLt2M) {
    ElMessage.error('图片大小不能超过2M!')
  }
  return isJPG && isLt2M
}


    </script>
    
    <style lang="scss" scoped>
    .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
    </style>
    